<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Merchant;
use App\Models\Floor;
use App\Models\MerchantCategory;
use App\Models\Search;
use App\Http\Controllers\Api\V1\Resources\MerchatResource;
use App\Http\Controllers\Api\V1\Resources\ActivityItemResource;

class MerchantsController extends Controller
{
    
   public function homeNewMechants()
    {
        $query = Merchant::query();

        $merchants = $query->where("is_shown",1)->orderBy('order')->take(3)->get();
        
       
        return MerchatResource::collection($merchants);
    }

    public function index()
    {
        $categoryId = request('category_id');
        $floorId = request('floor_id');
         $keyword = request('keyword');
        MerchatResource::wrap('data');

        $query = Merchant::query();

        
        if($categoryId){
            $query = $query->where('merchant_category_id',$categoryId);
        }
        if($floorId){
            $query = $query->where('floor_id', 'like','%'.$floorId.'%');
        }

        if($keyword){

              $query = $query->where('name', 'like','%'.$keyword.'%');
        }

         $merchants = $query->orderBy('order')->orderByDesc('id')->paginate();
        
       
        return MerchatResource::collection($merchants);
    }

    public function show($id)
    {

      $merchant = Merchant::find($id);
      return new  MerchatResource($merchant);
    }

    public function floors()
    {
        $floors = Floor::query()->orderBy("order")->get()->toArray();
        array_unshift($floors,['id'=>0,'name'=>"全部"]);
        return response()->json($floors);
    }

    public function categories()
    {
        $res = MerchantCategory::query()->orderBy("order")->get()->toArray();

        array_unshift($res,['id'=>0,'name'=>"全部"]);
        
         return response()->json($res);
    }
}
